package cn.itcast.jx.action.sysadmin;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.domain.Group;
import cn.itcast.jx.domain.Module;
import cn.itcast.jx.domain.Role;
import cn.itcast.jx.domain.User;
import cn.itcast.jx.service.ModuleService;
import cn.itcast.jx.service.RoleService;
import cn.itcast.jx.util.Page;

public class RoleAction extends BaseAction implements ModelDriven<Role>{

	private Page page = new Page();
	
	private RoleService roleService;
	private Role model = new Role();
	private String menuType = "role";
	
	public String list() {
		String hql = "from Role";
		roleService.findPage(hql, page, Role.class, null);
		
		page.setUrl("roleAction_list");
		push(page);
		return "list";
	}
	public String toview() {
		Role role = roleService.get(Role.class, model.getId());
		push(role);
		return "toview";
	}
	public String tocreate() {
		return "tocreate";
	}
	public String insert() {
		roleService.saveOrUpdate(model);
		return "tolist";
	}
	public String toupdate() {
		Role role = roleService.get(Role.class, model.getId());
		push(role);
		return "toupdate";
	}
	public String update() {
		roleService.saveOrUpdate(model);
		return "tolist";
	}
	public String delete() {
		
		String[] ids = model.getId().split(", ");
		
		if(ids.length == 1){
			roleService.deleteById(Role.class, ids[0]);
		}else {
			roleService.delete(Role.class, ids);
		}
		
		return "tolist";
	}
	
	public String tomodule() {
		
		Role role = roleService.get(Role.class, model.getId());
		push(role);
		
		return "tomodule";
	}
	
	public String roleModuleJsonStr() {
		
		String hql = "from Module where state = 1";
		List<Module> modules = moduleService.find(hql, Module.class, null);
		
		Role role = roleService.get(Role.class, model.getId());
		
		Set<Module> moduleSet = role.getModules();
		/*
		 * [
		 * 	{id:xx,pId:x,name:xxxx},
		 * 	{},
		 * ]
		 */
		String str = "[";
		for (Module module : modules) {
			str +="{id:'"+module.getId()+"',pId:'"+(module.getParent() == null? null:module.getParent().getId())+"',name:'"+module.getName()+"'";
			if (moduleSet.contains(module)) {
				str += ",checked:true,open:true";
			}
			
			str += "},";
		}
		str = str.substring(0, str.length()-1);
	
		str+="]";
		//使用reponse向前台写数据
		HttpServletResponse response = ServletActionContext.getResponse();
		//设置字符集编码和数据格式
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("cache-control", "no-cache");
		//写
		try {
			response.getWriter().write(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		return NONE;
	}
	
	public String module() {
		Role role = roleService.get(Role.class, model.getId());
		
		Set<Module> modules = new HashSet<>();
		
		String[] ids = moduleIds.split(",");
		for (String id : ids) {
			modules.add(moduleService.get(Module.class, id));
		}
		role.setModules(modules);
		
		roleService.saveOrUpdate(role);
		
		return "tolist";
	}
	
	
	@Override
	public Role getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	public String getMenuType() {
		return menuType;
	}
	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}


	private ModuleService moduleService;
	
	private String moduleIds;
	public String getModuleIds() {
		return moduleIds;
	}
	public void setModuleIds(String moduleIds) {
		this.moduleIds = moduleIds;
	}
	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
	